package com.huang.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huang.eduservice.client.VodClient;
import com.huang.eduservice.entity.EduVideo;
import com.huang.eduservice.service.EduVideoService;
import com.huang.servicebase.execptionhandler.huangException;
import com.hunag.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-21
 */
@RestController
@RequestMapping("/eduservice/edu-video")
public class EduVideoController {

    @Autowired
    private EduVideoService service;

    @Autowired
    private VodClient vodClient;

    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        service.save(eduVideo);
        return R.ok();
    }

    //以后完善  删除小节  传小节id
    @DeleteMapping("deleteVideo/{id}")
    public R deleteVideo(@PathVariable String id) {

        String videoSourceId = service.getById(id).getVideoSourceId();
        System.out.println(videoSourceId);
        if (!StringUtils.isEmpty(videoSourceId)) {
            R result = vodClient.removeAliyunVideo(videoSourceId);//服务调用
            if (result.getCode() == 20001) {
                throw new huangException(20001, "删除视频失败");
            }
        }
        service.removeById(id);
        return R.ok();
    }

    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("id", eduVideo.getId());
        service.update(eduVideo, wrapper);
        return R.ok();

    }

    @GetMapping("getVideoByid/{videoId}")
    public R getVideoByid(@PathVariable String videoId) {
        EduVideo eduVideo = service.getById(videoId);
        return R.ok().data("eduVideo", eduVideo);
    }
}

